package com.edu.admin.entity;


import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;

import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.edu.admin.common.BaseEntity;

/**
 * 管理员表实体
 * 
 * @author Wangjiong
 * 
 */
@Table(name = "sys_admin")
public class SysAdmin  extends BaseEntity implements UserDetails{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -1053815246499525852L;
	/**
	 * 管理员编号
	 * 
	 */
	@Id
	@GeneratedValue(generator = "JDBC")
	private Long adminId;
	/**
	 * 管理员账号
	 * 
	 */
	private String account;
	/**
	 * 管理员姓名
	 * 
	 */
	@NotBlank(message = "管理员姓名不能为空")
	private String adminName;
	/**
	 * 管理员电话
	 * 
	 */
	@NotBlank(message = "管理员电话不能为空")
	private String phone;
	/**
	 * 管理员密码
	 * 
	 */
	private String password;
	/**
	 * 管理员角色id
	 * 
	 */
	@Min(value = 1, message = "角色不能为空") 
	private Long roleId;
	/**
	 * 学校id
	 * 
	 */
	private Long schoolId;
	/**
	 * 班级id
	 * 
	 */
	private Long classesId;
	/**
	 * 父级管理者id
	 * 
	 */
	private Long fatherId;
	/**
	 * 省份Code
	 * 
	 */
	private String provinceCode;
	/**
	 * 城市Code
	 * 
	 */
	private String cityCode;
	/**
	 * 区县Code
	 * 
	 */
	private String districtCode;
	/**
	 * 
	 * 
	 */
	private Integer type;
	/**
	 * 最近登录日期
	 * 
	 */
	private String loginDate;
	/**
	 * 是否使用
	 * 
	 */
	private Integer useType;
	/**
	 * token 
	 * 
	 */
	private String token ;
	/**
	 * 学校名称
	 * 
	 */
	@Transient
	private String schoolName;
	/**
	 * 角色名称
	 * 
	 */
	@Transient
	private String roleName;
	/**
	 * 角色层级
	 * 
	 */
	@Transient
	private String roleLevel;
	/**
	 * 权限
	 * 
	 */
	@Transient
	private Collection<? extends GrantedAuthority> authorities;
	/**
	 * roleUrls
	 * 
	 */
	@Transient
	private String roleUrls;
	/**
	 * 可访问MenuURL
	 * 
	 */
	@Transient
	private String firstMenuUrl;
	
	public Long getAdminId() {
		return adminId;
	}
	public void setAdminId(Long adminId) {
		this.adminId = adminId;
	}
	
	public String getAccount() {
		return account;
	}
	public void setAccount(String account) {
		this.account = account;
	}
	public String getAdminName() {
		return adminName;
	}
	public void setAdminName(String adminName) {
		this.adminName = adminName;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	@Override
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Long getRoleId() {
		return roleId;
	}
	public void setRoleId(Long roleId) {
		this.roleId = roleId;
	}
	public Long getSchoolId() {
		return schoolId;
	}
	public void setSchoolId(Long schoolId) {
		this.schoolId = schoolId;
	}
	public Long getClassesId() {
		return classesId;
	}
	public void setClassesId(Long classesId) {
		this.classesId = classesId;
	}
	public Long getFatherId() {
		return fatherId;
	}
	public void setFatherId(Long fatherId) {
		this.fatherId = fatherId;
	}
	public String getProvinceCode() {
		return provinceCode;
	}
	public void setProvinceCode(String provinceCode) {
		this.provinceCode = provinceCode;
	}
	public String getCityCode() {
		return cityCode;
	}
	public void setCityCode(String cityCode) {
		this.cityCode = cityCode;
	}
	public String getDistrictCode() {
		return districtCode;
	}
	public void setDistrictCode(String districtCode) {
		this.districtCode = districtCode;
	}
	public Integer getType() {
		return type;
	}
	public void setType(Integer type) {
		this.type = type;
	}
	public String getLoginDate() {
		return loginDate;
	}
	public void setLoginDate(String loginDate) {
		this.loginDate = loginDate;
	}
	public Integer getUseType() {
		return useType;
	}
	public void setUseType(Integer useType) {
		this.useType = useType;
	}
	public String getSchoolName() {
		return schoolName;
	}
	public void setSchoolName(String schoolName) {
		this.schoolName = schoolName;
	}
	public String getRoleName() {
		return roleName;
	}
	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}
	public String getRoleLevel() {
		return roleLevel;
	}
	public void setRoleLevel(String roleLevel) {
		this.roleLevel = roleLevel;
	}
	
	public String getToken() {
		return token;
	}
	public void setToken(String token) {
		this.token = token;
	}
	
	public void setAuthorities(Collection<? extends GrantedAuthority> authorities) {
		this.authorities = authorities;
	}
	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {

        return authorities;
	}
	@Override
	public String getUsername() {
		return account;
	}
	@Override
	public boolean isAccountNonExpired() {
		return true;
	}
	@Override
	public boolean isAccountNonLocked() {
		return true;
	}
	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}
	@Override
	public boolean isEnabled() {
		if (useType==0)
		{
			return true;
		}else{
			return false;
		}
	}
	
	public String getAuthoritiesString() {  
	    List<String> authorities = new ArrayList<String>();  
	    for(GrantedAuthority authority : this.getAuthorities()) {  
	        authorities.add(authority.getAuthority());  
	    }  
	    return StringUtils.join(authorities, ",");  
	}
	public String getRoleUrls() {
		return roleUrls;
	}
	public void setRoleUrls(String roleUrls) {
		this.roleUrls = roleUrls;
	}
	public String getFirstMenuUrl() {
		return firstMenuUrl;
	}
	public void setFirstMenuUrl(String firstMenuUrl) {
		this.firstMenuUrl = firstMenuUrl;
	}
		

}
